/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Supplier;
import me.zhengjie.modules.system.service.SupplierAuditHistoryService;
import me.zhengjie.modules.system.service.SupplierService;
import me.zhengjie.modules.system.service.dto.SupplierAuditHistoryQueryCriteria;
import me.zhengjie.modules.system.service.dto.SupplierQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.enums.AuditEnum;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author berry
 * @website https://docs.auauz.net
 * @date 2020-06-06
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "supplier管理")
@RequestMapping("/api/supplier")
public class SupplierController {

    private final SupplierService supplierService;
    private final SupplierAuditHistoryService supplierAuditHistoryService;

    @GetMapping(value = "/download")
    @Log("导出数据")
    @ApiOperation("导出数据")
    @PreAuthorize("@el.check('supplier:list')")
    public void download(HttpServletResponse response, SupplierQueryCriteria criteria) throws IOException {
        supplierService.download(supplierService.queryAll(criteria), response);
    }

    @GetMapping(value = "/all")
    @Log("查询supplier")
    @ApiOperation("查询supplier")
    @PreAuthorize("@el.check('supplier:list')")
    public ResponseEntity<Object> queryAll(SupplierQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(supplierService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/audit/all")
    @Log("查询supplier审核记录")
    @ApiOperation("查询supplier审核记录")
    @PreAuthorize("@el.check('supplier_audit:list')")
    public ResponseEntity<Object> queryAuditAll(SupplierAuditHistoryQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(supplierAuditHistoryService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping
    @Log("查询supplier")
    @ApiOperation("查询supplier")
    @PreAuthorize("@el.check('supplier:list')")
    public ResponseEntity<Object> query(SupplierQueryCriteria criteria, Pageable pageable) {
        criteria.setCreateBy(SecurityUtils.getCurrentUsername());
        return new ResponseEntity<>(supplierService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增supplier")
    @ApiOperation("新增supplier")
    @PreAuthorize("@el.check('supplier:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Supplier resources) {
        return new ResponseEntity<>(supplierService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改supplier")
    @ApiOperation("修改supplier")
    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Supplier resources) {
        supplierService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/status")
    @Log("修改supplier状态")
    @ApiOperation("修改supplier状态")
    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> updateStatus(@Validated @RequestBody Supplier.RequestStatus requestStatus) {
        supplierService.updateStatus(requestStatus);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除supplier")
    @ApiOperation("删除supplier")
    @PreAuthorize("@el.check('supplier:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        supplierService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("上传营业执照")
    @ApiOperation("修改头像")
    @PreAuthorize("@el.check('supplier:add')")
    @PostMapping(value = "/upload-license")
    public ResponseEntity<Object> uploadLicense(@RequestParam("file") MultipartFile license) {
        return new ResponseEntity<>(supplierService.uploadLicense(license), HttpStatus.OK);
    }
}